package com.dhcc.sds.busi.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import com.dhcc.sds.busi.dto.SdsCaseDto;
import com.dhcc.sds.busi.service.ISdsCaseService;
import com.dhcc.sds.busi.service.ISdsFillByDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${sys.backendPath}/sds/case")
public class SdsCaseController extends BaseController {
    private String PREFIX = "/backend/sds/case/";

    @Autowired
    private SysProperties sysPro;
    @Autowired
    private ISdsCaseService sdsCaseService;
    @Autowired
    private ICdcHospitalService cdcHospitalService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;
    @Autowired
    private ISdsFillByDoctorService sdsFillByDoctorService;

    @RequestMapping("")
    public String caseListPage(Model model) {
        //获取部门信息
        JSONArray officMap = cdcHospitalService.getOfficeMap();
        //加载用户关联科室
        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        //获取病种
        JSONArray entityMap = sdsQcentityService.getUpEntityMap();

        model.addAttribute("officeMap", officMap);
        model.addAttribute("departMap", departMap);
        model.addAttribute("entityMap", entityMap);
        model.addAttribute("hospitalId", sdsCommonService.getHospIdBySysUser());
        return PREFIX + "patientlist.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object caseList(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        Page<SdsCaseDto> page = new PageFactory<SdsCaseDto>().defaultPage();
        List<SdsCaseDto> result = this.sdsCaseService.page(page, params);
        page.setRecords((List<SdsCaseDto>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
    
    @RequestMapping("/list2")
    @ResponseBody
    public Object caseList2(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> caseResultArray = this.sdsCaseService.page2(page, params);
        page.setRecords((List<Map<String, Object>>) new EmptyWarpper(caseResultArray).warp());
        return R.packForBT(page);
    }
    
    @RequestMapping("/getCaseColumn")
    @ResponseBody
    public Object getCaseColumn(@RequestParam Map<String, Object> params) {
        return this.sdsCaseService.getCaseColumn(params);
    }

    /**
     * @Desc 生成excel文件
     * @param params
     * @return
     * @Author syj
     * @Date 2022年05月31日07:53:00
     */
    @RequestMapping(value = "/createExcel")
    @ResponseBody
    public Object createExcel(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        String fileName = "";
        if (CommonUtil.isEmpty(params.get("entityType"))) {
            fileName = this.sdsCaseService.createExcel(params);
        } else {
            fileName = this.sdsCaseService.createExcel2(params);
        }
        return R.ok(fileName);
    }

    /**
     * @Desc 下载生成的Excel文件  .+ 后面的解决小数点取值不全问题
     * @param fileName
     * @param resp
     * @throws IOException
     * @Author wujiafeng
     * @Date 2018年11月14日 上午11:26:34
     */
    @RequestMapping(value = "excelDown/{fileName:.+}")
    @ResponseBody
    public void excelDown(@PathVariable String fileName, HttpServletResponse resp) throws IOException{
        String filePath = sysPro.getSysHome()+"file_download/sds/case/";
        String excelFile = filePath + fileName;
        File file = new File(excelFile);
        if(file.exists()){
            //将文件名设置为中文
            byte[] tmpb = fileName.getBytes("GBK");
            String fileNameCN = new String(tmpb, "8859_1");
            resp.setContentType("application/octet-stream");
            resp.setHeader("Content-Disposition","attachment;filename=" + fileNameCN);
            resp.setContentLength((int) file.length());
            //打开指定文件流
            BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(excelFile));
            //写出流信息
            byte[] bytes =new byte[1024];
            OutputStream os = resp.getOutputStream();
            BufferedOutputStream fosObject = new BufferedOutputStream(os);
            while (fileInputStream.read(bytes)!=-1) {
                fosObject.write(bytes);
            }
            fileInputStream.close();
            fosObject.close();
            os.flush();
            os.close();
        }
    }
}
